const month_olympic = [31,29,31,30,31,30,31,31,30,31,30,31];
const month_normal = [31,28,31,30,31,30,31,31,30,31,30,31];
const month_name = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
const my_date = new Date();
const my_year = my_date.getFullYear();
const my_month = my_date.getMonth();
const my_day = my_date.getDate();
class Calender {
	constructor(arg) {
		this.my_time = "00:00"
		this.select_month = "";
		this.select_year = "";
	}
	// 初始化
	init(){
		// 添加点击事件
		const ele = ["ida-left","ia-left","ia-right","ida-right"];
		ele.map(item=>{
			this.getElement(item).addEventListener('click',()=>{
				switch(item){
					case "ida-left":
					this.lastYear();
					break;
					case "ia-left":
					this.lastMonth();
					break;
					case "ia-right":
					this.nextMonth();
					break;
					case "ida-right":
					this.nextYear();
					break;
				}
			})
		});
		this.refreshDate();// 执行时间
		this.getNowTime();// 获取当前时间
	}
	// 获取节点
	getElement(id){
		return document.getElementById(id)
	}
	// 获取某年某月第一天是星期几
	dayStart(month, year) {
		let tmpDate = new Date(year, month, 1);
		return (tmpDate.getDay());
	}
	// 计算某年是不是闰年，通过求年份除以4的余数即可
	daysMonth(month, year) {
		let tmp = year % 4;
		let typeMonth = month_olympic[month];
		if(tmp != 0){
			typeMonth = month_normal[month];
		}
		return typeMonth;
	}
	// 执行时间
	refreshDate(isFirst,input_month,input_year){
		let str = "";
		let month = my_month;
		let year = my_year;
		if(isFirst){
			this.select_month = my_month;
			this.select_year = my_year;
		}
		if(input_month != null && input_month != ""){
			month=parseInt(input_month)
		}
		if(input_year != null && input_year != ""){
			year=parseInt(input_year)
		}
		let totalDay = this.daysMonth(month, year); //获取该月总天数
		let firstDay = this.dayStart(month, year); //获取该月第一天是星期几
		let myclass;
		for(let i=1; i<firstDay; i++){
			str += "<li></li>"; //为起始日之前的日期创建空白节点
		}
		for(let i=1; i<=totalDay; i++){
			if((i<my_day && year==my_date.getFullYear() && month==my_date.getMonth()) || year<my_date.getFullYear() || ( year==my_date.getFullYear() && month<my_date.getMonth())){ 
				myclass = " class='lightgrey'"; //当该日期在今天之前时，以浅灰色字体显示
			}else if (i==my_day && year==my_date.getFullYear() && month==my_date.getMonth()){
				let motion = " onclick='selectDate(this)'";
				myclass = " class='blue bluebox'" +motion; //当天日期以蓝色背景突出显示
			}else{
				let motion = " onclick='selectDate(this)' onmousemove= \"this.style.cursor='pointer';this.style.border='1px solid #00B0E8'\" onmouseout=\"this.style.border='0px'\""
				myclass = " class='darkgrey'"+motion; //当该日期在今天之后时，以深灰字体显示
			}
			
			str += "<li"+myclass+">"+i+"</li>"; //创建日期节点
		}
		this.printElement(str,month_name[month],year);
	}
	// 打印日期
	printElement(str,month,year){
		this.getElement('days').innerHTML = str;
		this.getElement('calendar-title').innerHTML = month;
		this.getElement('calendar-year').innerHTML = year;
	}
	// 获取当前时间
	getNowTime(){
		// console.log(my_date.getHours())
		if(my_date.getHours()<10  && my_date.getMinutes()<10){
			this.my_time = `0${my_date.getHours()}:0${my_date.getMinutes()}`;
		}
		if(my_date.getHours()>=10 && my_date.getMinutes()>=10){
			this.my_time = `${my_date.getHours()}:${my_date.getMinutes()}`;
		}
		if(my_date.getHours()<10 && my_date.getMinutes()>=10){
			this.my_time = `0${my_date.getHours()}:${my_date.getMinutes()}`;
		}
		if(my_date.getHours()>=10 && my_date.getMinutes()<10){
			this.my_time = `${my_date.getHours()}:0${my_date.getMinutes()}`;
		}
		$("#time").val(this.my_time);
		console.log(this.my_time)
	}	
	// 上个月
	lastMonth(){
		if(this.select_month>0){
			this.select_month = this.select_month-1
		}else{
			this.select_month = 11;
			this.select_year = this.select_year-1; 
		}
		this.refreshDate(false,this.select_month.toString(),this.select_year.toString()); 
	}
	// 下个月
	nextMonth(){
		if(this.select_month<11){
			this.select_month = this.select_month+1
		}else{
			this.select_month = 0;
			this.select_year = this.select_year+1;
		}
		this.refreshDate(false,this.select_month.toString(),this.select_year.toString());
	}
	// 上一年
	lastYear(){
		if(this.select_year<=1900){
			this.select_year = 1900;
		}else{
			this.select_year = this.select_year-1;
		}
		this.refreshDate(false,this.select_month,this.select_year.toString());
	}
	// 下一年
	nextYear(){
		if(this.select_year>=3000){
			this.select_year = 3000;
		}else{
			this.select_year = this.select_year+1;
		}
		this.refreshDate(false,this.select_month,this.select_year.toString());
	}
};
const calender = new Calender();
calender.init();// 执行初始化
// 点击时添加蓝色
function selectDate(that){
	$(that).addClass("blue").siblings().removeClass("blue");
}